from django.contrib.auth.models import Permission, ContentType

from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from meiduo_admin.serializer.permission import PermissionSerializer, ContentTypeSerialzier
from meiduo_admin.utils import UserPageNum


class PermissionView(ModelViewSet):

    # 获取所有的权限
    queryset = Permission.objects.all()
    # 指定序列化器
    serializer_class = PermissionSerializer
    #使用分页器
    pagination_class = UserPageNum
    # 权限认证
    permission_classes = [IsAdminUser]

    # 获取权限类型数据
    def content_types(self, request):
        # 查询全选分类
        content = ContentType.objects.all()
        # 返回结果
        ser = ContentTypeSerialzier(content, many=True)
        return Response(ser.data)